﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem
{
    public partial class UserManagerWindow : ChildWindow
    {

        SystemService.SystemServiceClient Client = new SystemService.SystemServiceClient();
        private EmpbasicService.EmpBasicServiceClient Client1 = new EmpbasicService.EmpBasicServiceClient();
        SystemService.UserInfo UserInfoModel = new SystemService.UserInfo();
        public ObservableCollection<EmpbasicService.OrgForHRModel> OrgList =
            new ObservableCollection<EmpbasicService.OrgForHRModel>();
        //UserInfoModel
        /// <summary>
        /// 
        /// </summary>
        public UserManagerWindow()
        {
            InitializeComponent();
            this.Title = "新增用户";
            orgids.Clear();
            Client1.GetOrgForHRModelCompleted -= new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client1.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client1.GetOrgForHRModelAsync(0);
            UserInfoModel.Birthday = DateTime.Now;
            this.DataContext = UserInfoModel;

        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public UserManagerWindow(SystemService.UserInfo UserInfoModel1)
        {

            InitializeComponent();
            orgids.Clear();
            UserInfoModel = UserInfoModel1;
            this.Title = "编辑用户";
            if (!string.IsNullOrEmpty(UserInfoModel1.OrgCode))
            {
                foreach (string i in UserInfoModel1.OrgCode.Split(','))
                {
                    orgids.Add(i);
                }
            }
            
            

            if (UserInfoModel.Gender == 0)
                XB1.IsChecked = true;
            else
                XB2.IsChecked = true;
            Client1.GetOrgForHRModelCompleted -= new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client1.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client1.GetOrgForHRModelAsync(0);
            this.DataContext = UserInfoModel;
        }



        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (true == XB1.IsChecked)
                UserInfoModel.Gender = 0;
            else
                UserInfoModel.Gender = 1;
            if(orgids.Count > 0)
            {
                UserInfoModel.OrgCode = "";
                foreach(string i in orgids)
                {
                    UserInfoModel.OrgCode += i + ",";
                }
            }
            UserInfoModel.OrgCode = UserInfoModel.OrgCode.Substring(0, UserInfoModel.OrgCode.Length - 1);
            //编辑
            if (UserInfoModel.UserID > 0)
            {
                Client.UpdateUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_UpdateUserCompleted);
                Client.UpdateUserAsync(UserInfoModel);
            }
            else
            {
                //新增
                
                UserInfoModel.CreateDate = DateTime.Now;
                Client.InsertUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_InsertUserCompleted);
                Client.InsertUserAsync(UserInfoModel);
            }


            this.DialogResult = true;
        }


        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_UpdateUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            this.DialogResult = true;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_InsertUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        #region 组织机构树
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();
        public string IDS = "";
        public List<string> orgids = new List<string>();
        public String[] Oldorgids;
        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            OrgList = e.Result;
            forumList.Clear();
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            if (!string.IsNullOrEmpty(UserInfoModel.OrgCode))
            {
                Oldorgids = UserInfoModel.OrgCode.Split(',');
            }
            AddTreeNode("全部单位", null);
        }
        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();

                    //StackPanel sp = new StackPanel();
                    CheckBox cb = new CheckBox();
                    if (Oldorgids != null)
                    {
                        foreach (string i in Oldorgids)
                        {
                            if (foruminfo.ID == Convert.ToInt32(i))
                                cb.IsChecked = true;
                        }
                    }
                    
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;

                    //objTreeNode.Header = foruminfo.Name;
                    //objTreeNode.DataContext = foruminfo;
                    //objTreeNode.Tag = foruminfo.ID;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;

                    //此样式将会添加的所有叶子结点上
                    //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                if (orgids != null)
                {
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                else orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());

                //CheckParentItem(cb.Tag as TreeViewItem);
                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
        }
        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        #endregion
    }
}

